/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    DongPeng
 * 创建时间:    2023-10-10
 * copyright(c) 2022 无锡星智数服科技有限公司
 */
package org.jeecg.modules.dock.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.dock.client.NestRequestClient;
import org.jeecg.modules.dock.config.JsonUtils;
import org.jeecg.modules.dock.constant.SystemConstants;
import org.jeecg.modules.dock.entity.DockDataSendRecord;
import org.jeecg.modules.dock.entity.nest.NestBaseRequestVO;
import org.jeecg.modules.dock.entity.nest.NestUpdateStatusVO;
import org.jeecg.modules.dock.enums.DockDataStatus;
import org.jeecg.modules.dock.service.IDockDataSendRecordService;
import org.jeecg.modules.dock.service.NestDockingRepeaterService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 套裁业务对接服务实现
 *
 * @author DongPeng
 * @version 1.0.0
 * @date 2023-10-10
 * @description 类描述
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class NestDockingRepeaterServiceImpl implements NestDockingRepeaterService {

    private final NestRequestClient nestRequestClient;

    private final IDockDataSendRecordService dockDataSendRecordService;

    @Override
    public NestUpdateStatusVO updateStatus(NestBaseRequestVO requestVO) {
        DockDataSendRecord record = getRecord(JsonUtils.toJson(requestVO));
        NestUpdateStatusVO nestBaseResponseVO = null;
        try {
            nestBaseResponseVO = nestRequestClient.updateStatus(requestVO);
            log.info("请求套裁成功,响应:{}",nestBaseResponseVO);
            if ("E".equals(nestBaseResponseVO.getHeader().getCode())){
                record.setStatus(DockDataStatus.FAILURE);
            }else {
                record.setStatus(DockDataStatus.SUCCESS);
            }
        }catch (Exception e){
            log.error("请求套裁失败,",e);
            record.setStatus(DockDataStatus.FAILURE);
        }
        if (ObjectUtil.isNotNull(nestBaseResponseVO)){
            record.setResultData(nestBaseResponseVO.toString());
        }
        dockDataSendRecordService.save(record);
        return nestBaseResponseVO;
    }



    private DockDataSendRecord getRecord(String data) {
        DockDataSendRecord sendRecord = new DockDataSendRecord();
        sendRecord.setSource(SystemConstants.WMS);
        sendRecord.setTarget(SystemConstants.NEST);
        sendRecord.setUuid(IdUtil.fastSimpleUUID());
        sendRecord.setSendTime(new Date());
        sendRecord.setData(data);
        return sendRecord;
    }
}
